//
//  QPayOutChargeTypeVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/17.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QPayOutChargeTypeVC.h"

@interface QPayOutChargeTypeVC ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation QPayOutChargeTypeVC{

    NSDictionary *_dicData;
    NSArray *_allKeys;
    UITableView *_tableView_1;
    UITableView *_tableView_2;
    
    NSInteger _index;//点第一个tableview的索引
    NSInteger _index_2;//点第二个tableview的索引
    NSMutableArray *_dataArr;//回传的数据
}

- (instancetype)initWithBlock:(Dblock)block{
    self = [super init];
    if(self){
        _block = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支出费用类型";
    
    _index = 0;
    _index_2 = 0;
    _dicData = @{@"工程费用":@[@"燃油费用",@"配件费用",@"维修费用",@"保养费用",@"设备月供",@"其他"],@"业务费用":@[@"招待费用",@"礼金红包",@"物品费用",@"其他"],@"个人费用":@[@"停车费用",@"过路过桥",@"公共交通",@"出租车",@"其他"]};
    _allKeys = @[@"工程费用",@"业务费用",@"个人费用",];
    _dataArr = [NSMutableArray array];
    [_dataArr addObject:_allKeys[0]];
    [self createUI];
}

- (void)createUI{
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableView_1 = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavHeight+kStatusHeight, 145, kScreenHeight-kNavHeight-kStatusHeight) style:UITableViewStyleGrouped];
    _tableView_1.dataSource = self;
    _tableView_1.delegate = self;
    _tableView_1.tag = 1;
    _tableView_1.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView_1.layer.borderColor = [QColorUtility defaultLineColor].CGColor;
    _tableView_1.layer.borderWidth = 0.4;
    _tableView_1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView_1];
    
    _tableView_2 = [[UITableView alloc] initWithFrame:CGRectMake(_tableView_1.right,kNavHeight+kStatusHeight, kScreenWidth-145, kScreenHeight-kNavHeight-kStatusHeight) style:UITableViewStyleGrouped];
    _tableView_2.dataSource = self;
    _tableView_2.delegate = self;
    _tableView_2.tag = 2;
    _tableView_2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView_2];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(tableView.tag == 1){
        return _allKeys.count;
    }else{
        NSString *key = _allKeys[_index];
        NSArray *arrValue = _dicData[key];
        return arrValue.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    if(tableView.tag == 1){
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if(!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
        }
        if(indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"gcCharge_image"];
        }else if (indexPath.row == 1){
            cell.imageView.image = [UIImage imageNamed:@"ywCharge_image"];
        }else if (indexPath.row == 2){
            cell.imageView.image = [UIImage imageNamed:@"mySelfCharge_image"];
        }
        cell.textLabel.font = [UIFont boldSystemFontOfSize:Ksize16];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.text = _allKeys[indexPath.row];
        return cell;
    }else{
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if(!cell){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
        }
        cell.textLabel.textColor = [QColorUtility defaultBackColor];
        cell.textLabel.font = [UIFont systemFontOfSize:Ksize16];
        NSString *key = _allKeys[_index];
        NSArray *arrValue = _dicData[key];
        cell.textLabel.text = arrValue[indexPath.row];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if(tableView.tag == 1){
        [_dataArr removeAllObjects];
        _index = indexPath.row;
        [_dataArr addObject:_allKeys[_index]];
      [_tableView_2 reloadData];
    }else if (tableView.tag == 2){
        _index_2 = indexPath.row;
        
        NSString *key = _allKeys[_index];
        NSArray *arrValue = _dicData[key];
        [_dataArr addObject:arrValue[_index_2]];
        if(_block){
            NSString *tid = [NSString stringWithFormat:@"%ld%ld",_index+1,_index_2+1];
            _block(_dataArr,tid);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
